package com.niguang.labour.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.niguang.labour.admin.entity.AdminLaborCompanySite;
import com.niguang.labour.admin.entity.WorkAppUser;
import com.niguang.labour.admin.entity.WorkTeamUser;
import com.niguang.labour.admin.entity.WorkType;
import com.niguang.labour.admin.mapper.AdminLaborCompanySiteMapper;
import com.niguang.labour.admin.mapper.WorkTypeMapper;
import com.niguang.labour.admin.service.AdminLaborCompanySiteService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.niguang.labour.admin.service.AdminLaborUserService;
import com.niguang.labour.admin.service.WorkAppUserService;
import com.niguang.labour.admin.service.WorkTeamUserService;
import com.niguang.labour.common.constant.ResultCode;
import com.niguang.labour.common.pojo.Result;
import com.niguang.labour.common.util.ApiTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 劳务公司工地关联关系表 服务实现类
 * </p>
 *
 * @author
 * @since 2018-06-10
 */
@Service
public class AdminLaborCompanySiteServiceImpl extends ServiceImpl<AdminLaborCompanySiteMapper, AdminLaborCompanySite>
        implements AdminLaborCompanySiteService {

    @Autowired
    private ApiTokenUtils apiTokenUtils;

    @Autowired
    private AdminLaborUserService adminLaborUserService;

    @Autowired
    private WorkAppUserService workAppUserService;

    @Autowired
    private WorkTeamUserService workTeamUserService;

    @Autowired
    private WorkTypeMapper workTypeMapper;

    @Override
    public Result searchLabor(String siteName) {
        Long userId = apiTokenUtils.getUserId();
        return Result.build(ResultCode.SUCCESS, "项目搜索成功",
                baseMapper.searchLabor(siteName, userId));
    }

    @Override
    public Result searchSiteLabor(Long siteId) {
        Long userId = apiTokenUtils.getUserId();
        EntityWrapper<WorkTeamUser> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq(WorkTeamUser.USER_ID, userId).eq(WorkTeamUser.DEL_FLAG, 0).eq(WorkTeamUser.SITE_ID, siteId);
        List<WorkTeamUser> teamUserList = workTeamUserService.selectList(entityWrapper);
        if (CollectionUtils.isEmpty(teamUserList)) {
            return Result.build(ResultCode.SUCCESS, teamUserList);
        }
        List<Map<String, Object>> list = new ArrayList<>(teamUserList.size());
        teamUserList.forEach(p -> {
            WorkType workType = workTypeMapper.selectById(p.getWorkTypeId());
            Map<String, Object> typeMap = new HashMap<>(3);
            typeMap.put("teamId", p.getId());
            typeMap.put("bzName", p.getBzName());
            typeMap.put("workTypeName", workType.getName());
            list.add(typeMap);
        });
        return Result.build(ResultCode.SUCCESS, "项目搜索成功", list);
    }


    @Override
    public Result getLaborUser(String userPhone) {
        EntityWrapper<WorkAppUser> entityWrapper = new EntityWrapper<>();
        entityWrapper.like(WorkAppUser.USER_PHONE, userPhone)
                .eq(WorkAppUser.DEL_FLAG, 0).isNotNull(WorkAppUser.FACE_ID).last("limit 10");
        List<WorkAppUser> userList = workAppUserService.selectList(entityWrapper);
        if (CollectionUtils.isEmpty(userList)) {
            return Result.build(ResultCode.SUCCESS, "无用户");
        }
        List<Map<String, Object>> list = new ArrayList<>();
        userList.parallelStream().forEach(p -> {
            Map<String, Object> params = new HashMap<>(3);
            params.put("userId", p.getId());
            params.put("userName", p.getUserName());
            params.put("userPhone", p.getUserPhone());
            list.add(params);
        });
        return Result.build(ResultCode.SUCCESS, "搜索人员查询成功！", list);
    }
}
